package Lirmm.VSM;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.util.StringTokenizer;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

public class CreateCorpus
{

	int numberOfFiles;
	public static List<List<String>> MethodsVectors = new ArrayList<List<String>>();   //  list of methods' documents
	public static List<String> ListOfClasses = new ArrayList<String>();
	private List<String> ListOfMethods = new ArrayList<String>();
	SAXBuilder builder = new SAXBuilder();
	File xmlFile;
	private static int ConceptNo=0;
	private List<Lirmm.DataStructure.Document> ClassDocuments = new ArrayList<Lirmm.DataStructure.Document>();
			
	
	
	
	
	
	
	public   List<Lirmm.DataStructure.Document>  CreateCorpus(List<String> ClassList) throws IOException 
	{
		StringTokenizer st;
		List<String> ScenariosIm = new ArrayList<String>();
		MethodsVectors.clear();
		ListOfClasses.clear();
				
		for (String hk: ClassList )
		{
			ScenariosIm.add(hk);
		}
			
			 
		ConceptNo=ConceptNo+1;
		String ProductName = ScenariosIm.get(0);
		st=new StringTokenizer(ProductName, "@", false);
		ProductName=st.nextToken();
		
				
		
		
		
		{
			
				
			builder = new SAXBuilder();
			xmlFile = new File("res/" +ProductName + ".xml");
			
			try {
				Document document = (Document) builder.build(xmlFile);
				Element rootNode = document.getRootElement();
				List list = rootNode.getChildren("Packages");

				       
								
				for (int i = 0; i < list.size(); i++)
				{
					Element node = (Element) list.get(i);
					List nodeList = node.getChildren("Package"); // List of all
					  
                  
					for (int e = 0; e < nodeList.size(); e++) 
					{
						Element nodeClasses = (Element) nodeList.get(e);
						List nodeClassesList = nodeClasses.getChildren("Classes");
						  
						 						
						for (int w = 0; w < nodeClassesList.size(); w++) 
						{
							Element nodeClass = (Element) nodeClassesList.get(w);
							List nodeClassList = nodeClass.getChildren("Class"); // list of classes			
							
						for (int j = 0; j < nodeClassList.size(); j++) 
						{
								Element childNode = (Element) nodeClassList.get(j);
								
								if (ScenariosIm.contains(ProductName + "@"+ nodeClasses.getAttributeValue("PackageName")+"@"+childNode.getAttributeValue("ClassName")))  //    MethodName.equals(ProductName + "_"+ nodeClasses.getAttributeValue("PackageName")+"_"+childNode.getAttributeValue("ClassName")+"_"+childMethode.getAttributeValue("MethodName"))) && !BlackList.contains(MethodName)) 	
								
								{   
									
									Lirmm.DataStructure.Document TempDoc = new Lirmm.DataStructure.Document();
									
									int indx=ScenariosIm.indexOf(ProductName + "@"+ nodeClasses.getAttributeValue("PackageName")+"@"+childNode.getAttributeValue("ClassName"));  //    MethodName.equals(ProductName + "_"+ nodeClasses.getAttributeValue("PackageName")+"_"+childNode.getAttributeValue("ClassName")+"_"+childMethode.getAttributeValue("MethodName"));
								    
									ListOfClasses.add(ScenariosIm.get(indx));
									TempDoc.setTitle(ScenariosIm.get(indx));
									
									ScenariosIm.set(indx,"");
								        
									List<String> bufferMethod1 = new ArrayList<String>();
								    bufferMethod1.add(childNode.getAttributeValue("ClassName"));
								
								
								List nodeAttributeList = childNode.getChildren("Attributes");
								for (int m = 0; m < nodeAttributeList.size(); m++) 
								{
									Element AttributeNode = (Element) nodeAttributeList.get(m);
									List listAttributessNode = AttributeNode.getChildren("Attribute");
									for (int n = 0; n < listAttributessNode.size(); n++)
									{
										Element childAttribute = (Element) listAttributessNode.get(n);
										bufferMethod1.add(childAttribute.getAttributeValue("AttributeName"));
																			
									}
								}

								List nodeMethodList = childNode.getChildren("Methods");
								for (int mm = 0; mm < nodeMethodList.size(); mm++)
								{
									Element MethodeNode = (Element) nodeMethodList .get(mm);
									List listMethodsNode = MethodeNode.getChildren("Method");
									for (int nn = 0; nn < listMethodsNode.size(); nn++) 
									{
										Element childMethode = (Element) listMethodsNode.get(nn);
										
											bufferMethod1.add(childMethode.getAttributeValue("MethodName"));
																					
             							//--------------------------------------------------------------------
										
										List nodeParameterList = childMethode.getChildren("Parameters");
										for (int ip = 0; ip < nodeParameterList.size(); ip ++)
										{
											
									      	Element nodeParameters = (Element) nodeParameterList.get(ip);
									    	List nodeParameterListt = nodeParameters.getChildren("Parameter");
										
									    	for (int ipp = 0; ipp < nodeParameterListt.size(); ipp ++)
										      {
											    Element childParameters = (Element) nodeParameterListt.get(ipp);
											   	bufferMethod1.add(childParameters.getAttributeValue("ParameterName"));	
     										  }
																						
										}
										
										//--------------------------------------------------------------------------
										List nodeLocalVariablesList = childMethode.getChildren("LocalVariables");
										for (int ahu = 0; ahu < nodeLocalVariablesList.size(); ahu ++)
									     	{
											
										     Element nodeLocalVariables = (Element) nodeLocalVariablesList.get(ahu);
										     List LocalVariableList = nodeLocalVariables.getChildren("LocalVariable");
										
										     for (int ipp = 0; ipp < LocalVariableList.size(); ipp ++)
										        {
											       Element nodeLocalVariabl = (Element) LocalVariableList.get(ipp);
											    	bufferMethod1.add(nodeLocalVariabl.getAttributeValue("LocalVariableName"));	
												}
										     
									     	}										
										//--------------------------------------------------------------------------
										
										List nodeLocalMethodInvocationList = childMethode.getChildren("MethodInvocations");
										for (int ahu = 0; ahu < nodeLocalMethodInvocationList.size(); ahu ++)
										    {
											Element nodeMethodInvocations = (Element) nodeLocalMethodInvocationList.get(ahu);
										    List LocalMethodInvocation = nodeMethodInvocations.getChildren("MethodInvocation");
										
										    for (int ipp = 0; ipp < LocalMethodInvocation.size(); ipp ++)
										      {
											      Element nodeMethodInvocation = (Element) LocalMethodInvocation.get(ipp);
											      bufferMethod1.add(nodeMethodInvocation.getAttributeValue("MethodInvocationName"));
										  
										      }
																						
										    }
										
										//--------------------------------------------------------------------------------------
										List nodeLocalAccessFields = childMethode.getChildren("FieldAccesses");
										for (int ahuu = 0; ahuu < nodeLocalAccessFields.size(); ahuu ++)
										    {
											Element nodeFieldAccess = (Element) nodeLocalAccessFields.get(ahuu);
										    List nodeLocalAccessField = nodeFieldAccess.getChildren("FieldAccess");
										
										    for (int ippp = 0; ippp < nodeLocalAccessField.size(); ippp ++)
										      {
											      Element nodeField = (Element) nodeLocalAccessField.get(ippp);
											      //bufferMethod1.add(nodeField.getAttributeValue("FieldAccessName"));
										  
										      }
																						
										    }
										
										
										
										
										//}// end if for method
												
							         }
														   
						      }  // list of methods   
						      
								TempDoc.setTerms(bufferMethod1);
								ClassDocuments.add(TempDoc);
								
								//MethodsVectors.add(bufferMethod1);
								}
							} // end class loop
						
				        }
						
					}
				}

				
				
				
			} 
	
	
			catch (IOException io) 
			{
				System.out.println(io.getMessage());
			}
			
			catch (JDOMException jdomex)
			{
				System.out.println(jdomex.getMessage());

			}
			

		
		}		

		 
		
		//printMethodsContents(MethodsVectors,ListOfClasses);
		//System.out.println("No. of vectors : " + MethodsVectors.size());
		
		return ClassDocuments;

	}
	
		
  /* public void printMethodsContents(List<List<String>> ListVectors, List<String> MethodsName) throws IOException
   {
	   
	   File dir = new File("ConceptContents/Concept"+ConceptNo+"/");  dir.mkdir();
   	   removeDirectory("ConceptContents/Concept"+ConceptNo+"/");
   	   dir = new File("ConceptContents/Concept"+ConceptNo+"/");  dir.mkdir();
   	   
   	// System.out.println(ListVectors.size());
   	// System.out.println(MethodsName.size());
	   for (int i=0;i<MethodsName.size();i++)
	   {
		   
	   BufferedWriter bw = new BufferedWriter(	new FileWriter("ConceptContents/Concept"+ConceptNo+"/"+MethodsName.get(i)+".txt"));
	  
	   
	   for(int j=0;j<ListVectors.get(i).size();j++)
	    {
	    	bw.write(ListVectors.get(i).get(j));
	    	bw.newLine();
	    }
	   bw.close();
	   }
	   
   }*/
   
   public void removeDirectory(String Path)
	{
		File folder = new File(Path);
		
		File [] files = folder.listFiles();
		
		 
	   for (File f: files)
	          {
	  
		    f.delete();
		   
//		   if (!f.delete())
	//  
//	            {
//			  	   System.out.println("Failed to delete "+f);
//		        }
			   
			  
	        }
	   
	   folder.delete();
	}


}
